python-stdlib/enum: Add enum module#1061
Draft
andrewleech wants to merge 1 commit intomicropython:masterfrom
Draft
python-stdlib/enum: Add enum module#1061andrewleech wants to merge 1 commit intomicropython:masterfrom
andrewleech wants to merge 1 commit intomicropython:masterfrom
Conversation
Implements PEP 435 (Enum) and PEP 663 (Flag) with modular structure: - Enum, IntEnum with member management and value lookup - Flag, IntFlag with bitwise operations and combination membership - StrEnum for string-valued enums (Python 3.11+) - auto() for automatic value assignment - @unique decorator for duplicate value prevention Uses lazy loading to minimize memory footprint (~1.5KB core, ~2.5KB when all features loaded). Requires metaclass support: * MICROPY_PY_METACLASS_INIT for basic enums. * MICROPY_PY_METACLASS_PREPARE for auto() support. 99.3% compatible with CPython 3.13 enum test suite (445/448 tests pass). Includes CPython's official test_enum.py for validation. Signed-off-by: Andrew Leech <andrew.leech@planetinnovation.com.au>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds enum package implementing PEP 435 (Enum) and PEP 663 (Flag) with modular lazy-loading structure.
This package provides CPython-compatible enum support for MicroPython, enabling libraries like python-statemachine and providing standard enum functionality for embedded projects.
Features
Complete enum implementation:
Structure
Modular design with lazy loading to minimize memory footprint:
Total size when frozen: ~5.4KB (core always loaded: ~1.7KB, features loaded on demand: ~950 bytes)
CPython Compatibility
Tested against CPython 3.13's official test_enum.py:
Works
Not Implemented
Enum('Name', 'A B C')- use class syntax insteadKnown Limitation
IntEnum members fail
isinstance(member, int)check in MicroPython due to implementation constraints, but all integer operations work correctly. This is a MicroPython limitation, not an enum package issue.MicroPython Requirements
Requires metaclass support from main MicroPython repo:
These features are included in the companion PR to micropython/micropython: #18416
Usage
Testing
Includes CPython's official test_enum.py (6000+ lines) for validation. Run on MicroPython Unix port:
Size Impact
When frozen into firmware:
Example for STM32 PYBV10:
Related
Companion PR for MicroPython core: micropython/micropython#18416